<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\Request;

class ExamineController extends AdminBaseController
{
    public function shou()
    {
    }

    //查看注册审核列表
    public function index()
    {
        $id = session('ADMIN_ID');
        $user = Db::name('user')->where(['id' => $id, 'user_status' => 1])->find();
        if ($user['authority'] == 'admin' || $user['authority'] == '管理员' || $user['authority'] == '超级管理员')
        {
            $user_res = Db::name('user')->where('user_status', 2)->paginate(5, false, ['query' => request()->param()]);
        }
        //审核自己板块的人员
        if ($user['authority'] == '总经理')
        {
            $user_id = Db::name('portal_category')->where('parent_id', $user['branch_id'])->field('id')->select();
            $user_id_res = array();
            foreach ($user_id as $k => $v)
            {
                $user_id_res[] = $v['id'];
            }
            $user_res = Db::name('user')->whereIn('branch_id', $user_id_res)->where('user_status', 2)->paginate(5, false, ['query' => request()->param()]);
        }

        //审核自己板块的人员
        if ($user['authority'] == '项目总监')
        {
            $user_res = Db::name('user')->where(['user_status' => 2, 'branch_id' => $user['branch_id']])->paginate(5, false, ['query' => request()->param()]);
        }

        $this->assign(['user_res' => $user_res]);
        $this->assign(['user_list' => $user_res->render()]);
        return $this->fetch('examine');
    }

    //同意验证
    public function dateVali(Request $resquest)
    {
        $parameter = $resquest->param();
        $id = $parameter['id'];
        $res = Db::name('user')->where('id', $id)->update(['user_status' => 1, 'access_time' => time()]);
        if ($res)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    //删除账号
    public function deteUser(Request $resquest)
    {
        $parameter = $resquest->param();
        $id = $parameter['id'];
        $res = Db::name('user')->where('id', $id)->delete();
        if ($res)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    //查看登录审核列表
    public function asse()
    {
        $id = session('ADMIN_ID');
        $user = Db::name('user')->where(['id' => $id, 'user_status' => 1])->find();
        $authority = ['admin', '管理员', '总经理', '项目总监'];
        if ($user['authority'] == 'admin' || $user['authority'] == '管理员' || $user['authority'] == '超级管理员')
        {
            $user_res = Db::name('user')->whereNotIn('authority', $authority)->where('access_time', '<', time())->where('user_status', 1)->paginate(5, false, ['query' => request()->param()]);
        }
        //审核自己板块的人员
        if ($user['authority'] == '总经理')
        {
            $user_id = Db::name('portal_category')->where('parent_id', $user['branch_id'])->field('id')->select();
            $user_id_res = array();
            foreach ($user_id as $k => $v)
            {
                $user_id_res[] = $v['id'];
            }
            $user_res = Db::name('user')->whereNotIn('authority', $authority)->whereIn('branch_id', $user_id_res)->where('access_time', '<', time())->where('user_status', 1)->paginate(5, false, ['query' => request()->param()]);
        }

        //审核自己板块的人员
        if ($user['authority'] == '项目总监')
        {
            $user_res = Db::name('user')->whereNotIn('authority', $authority)->where('access_time', '<', time())->where(['branch_id' => $user['branch_id'], 'user_status' => 1])->paginate(5, false, ['query' => request()->param()]);
        }

        $this->assign(['user_res' => $user_res]);
        $this->assign(['user_list' => $user_res->render()]);
        return $this->fetch('asse');
    }

    //同意验证
    public function asseVali(Request $resquest)
    {
        $parameter = $resquest->param();
        $id = $parameter['id'];
        $time_count = time() + 2 * 60 * 60;
        $res = Db::name('user')->where('id', $id)->setField('access_time', $time_count);
        if ($res)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
}
